package com.lg.partition11;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/21 8:10
 */
public class P1038NOIP2003提高组_神经网络 {

    private static final int N = 110, M = N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M], din = new int[N], dout = new int[N];
    private static int[] f = new int[N], u = new int[N], q = new int[N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            f[i] = nextInt();
            u[i] = nextInt();
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt(), b = nextInt(), c = nextInt();
            add(a, b, c);
        }
        toposort();
        boolean flag = true;
        for (int i = 1; i <= n; i++) {
            if (f[i] > 0 && dout[i] == 0) {
                writer.format("%d %d\n", i, f[i]);
                flag = false;
            }
        }
        if (flag) writer.println("NULL");
        writer.flush();
    }

    private static void toposort() {
        int hh = 0, tt = -1;
        for (int i = 1; i <= n; i++) {
            if (din[i] == 0) q[++tt] = i;
        }
        while (hh <= tt) {
            int a = q[hh++];
            for (int i = h[a]; i != -1; i = ne[i]) {
                int b = e[i];
                din[b]--;
                f[b] += f[a] * w[i];
                if (din[b] == 0) {
                    f[b] -= u[b];
                    if (f[b] > 0) q[++tt] = b;
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
        dout[a]++;
        din[b]++;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
